<?php
class community_oauth_row extends system_concept_row {
	
	public function setAttribute($attr) {
		if($attr instanceof oauth_api_token) {
			$attr = $attr->toArray();
		} elseif (!is_array($attr)) {
			$attr = (array) $attr;
		}
		$this->set('attribute', serialize($attr));
	}
	
	/**
	 * @return oauth_api_token
	 */
	public function getAttribute() {
		try {
			$attr = unserialize($this->get('attribute'));
		} catch (Exception $e) {
			$attr = array();
		}
		return new oauth_api_token($attr);
	}
	
	/**
	 * @return system_setting_oauth
	 */
	public function getSource() {
		return new system_setting_oauth($this->get('source_id'));
	}
	
	public function setSource($source){
		$source = new system_setting_oauth($source);
		$this->set('source_id', $source->getId());
	}
	
	private $member = null;
	public function getMember($lang) {
		if($this->member === null || $this->member->getPassportId() != $this->get('passport_id')) {
			$concept = new community_member_concept($lang);
			$this->member = $concept->fetch(array('member.passport_id = ?' => $this->get('passport_id')));
		}
		return $this->member;
	}
	
	private $passport = null;
	private function getPassport() {
		if($this->passport === null || $this->passport->getId() != $this->get('passport_id')) {
			$this->passport = system_passport_instance::getRow($this->get('passport_id'));
		}
		return $this->passport;
	}
	
}
